<script>
import { translateDataToTree } from './../../utils/translateDataToTree'

export default {
  props: {},
  data() {
    return {
      data: [],
      options: [],
      dataObj: {}
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    async getCategoryList() {
      try {
        const res = await this.$api.config.queryConfigProductCategory({})
        res.forEach(item => {
          this.$set(item, 'value', item.id)
          this.$set(item, 'label', item.name)
          this.dataObj[item.id] = item.name
        })
        this.options = translateDataToTree(res)
        console.log(this.options)
      } catch (e) {
        console.log(e)
      }
    },
    changeOnSelect() {
      console.log(2)
    },
    cascaderChange(value) {
      console.log(value)
      const label = []
      value.forEach(_ => {
        label.push(this.dataObj[_])
      })
      this.$emit('input', value)
      this.$emit('change', value, label)
    }
  },
  render(h) {
    return (
      <a-cascader
        options={this.options}
        placeholder="请输入"
        style="width: 220px"
        v-model={this.data}
        onChange={value => this.cascaderChange(value)}
      />
    )
  }
}
</script>
